package com.pan.test.udp.example02;

import java.io.IOException;
import java.net.*;

public class ReceiverA {

    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(11451);

        byte[] question = "四大名著有哪些？".getBytes();
        int len = question.length;
        InetAddress inetAddress = InetAddress.getByName("192.168.31.43");
        DatagramPacket packet = new DatagramPacket(question,len,inetAddress,11452);
        socket.send(packet);

        byte[] buf = new byte[1024];
        DatagramPacket receiveDatagram = new DatagramPacket(buf,0,1024);
        socket.receive(receiveDatagram);
        byte[] data = receiveDatagram.getData();
        int dataLength = receiveDatagram.getLength();
        System.out.println(new String(data,0,dataLength));

        socket.close();

    }
}
